<?php session_start();?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<?php

include "Employee.php";
include "EmployeeDao.php";
include "Datasource.php";

$searchBox = $_POST["searchBox"];
$searchCriteria = $_POST["searchCriteria"];
$tempEmployee = new Employee();
$employeeDao = new EmployeeDao();
$conn = new Datasource("localhost","theanykey","root","");

switch ($searchCriteria)
{
    case "EmpID":
        $tempEmployee->setEmpID($searchBox);
        break;
    case "LastName":
        $tempEmployee->setLastName($searchBox);
        break;
    case "FirstName":
        $tempEmployee->setFirstName($searchBox);
        break;
}

$results = $employeeDao->searchMatching($conn, $tempEmployee);

?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="maintenancePagesCSS.css" />
        <title></title>
    </head>
    <body>
        <center>
            <form method="post" action="EmployeePopUpSearchFunc.php" id="EmployeeSearchForm">
                <table class="EditTable">
                    <thead>
                        <tr>
                            <th colspan="2">Employee Search</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="text" name="searchBox" value="<?php print($searchBox)?>" size="25" id="searchBox"/></td>
                            <td>
                                <select name="searchCriteria" id="searchCriteria">
                                    <option value="EmpID">Employee ID</option>
                                    <option value="LastName">Last Name</option>
                                    <option value="FirstName">First Name</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td><input type="button" value="Search" onclick = checkForm(); /></td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
            </form>


        </center>
        <?php
        // put your code here
        //$searchBox != "" <-- you are already doing this check client side w/ javascript
        // no need to recheck, lets recheck for a valid results set...
        if($results)
        {
        print("<center><h3>Search Results</h3>");
        print("<table class='MenuDiv2' cellspacing='5'>
                    <thead>
                        <tr>
                            <th></th>
                            <th>Employee ID</th>
                            <th>Last Name</th>
                            <th>First Name</th>
                            <th>Login ID</th>
                            <th>Role</th>
                            <th>Is Deleted</th>
                        </tr>
                    </thead>
                    <tbody>");

        foreach ($results as $temp)
        {
            print("<tr>
                        <td><a href='EmployeePost.php?id=".$temp->getEmpID()."'>Add</td>
                        <td>".$temp->getEmpID()."</td>
                        <td>".$temp->getLastName()."</td>
                        <td>".$temp->getFirstName()."</td>
                        <td>".$temp->getLoginID()."</td>
                        <td>".$temp->getRole()."</td>
                        <td>".$temp->getIsDeleted()."</td>
                  </tr>");

        }

        print("</tbody></table></center>");

        }
        ?>

    </body>
    <script type="text/javascript">
        function checkForm()
        {
            if((document.getElementById("searchBox").value == ""))
            {
                alert("Search field is blank.");
            }
            else
            {
                document.getElementById("EmployeeSearchForm").submit();
            }
        }

    </script>
</html>